package controller;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import model.Passenger;

import org.primefaces.event.RowEditEvent;

import service.PassengerService;

@ManagedBean
@SessionScoped
public class PassengerController {
	@EJB
	private PassengerService passengerService;
  
	private List<Passenger> allPassengers = null;

	private Passenger passenger = new Passenger();
	
	public Passenger getPassenger() {
		return passenger;
	}
	
	public List<Passenger> getAllPassengers() {
		if (allPassengers == null) {
			allPassengers = passengerService.findAll();
			return allPassengers;
		} else {
			return allPassengers;
		}
	}
	
	public void onEdit(RowEditEvent event) {  
		Passenger editPassenger = (Passenger) event.getObject();
		String result = passengerService.update(editPassenger);
		allPassengers = passengerService.findAll();
		FacesMessage msg = new FacesMessage(result);  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }  
      
    public void deletePassenger(Passenger passenger) {  
    	passengerService.delete(passenger);
    	allPassengers = passengerService.findAll();
    }  
    
    public void createPassenger() {  
    	String result = passengerService.save(this.passenger);
    	this.passenger = new Passenger();
    	allPassengers = passengerService.findAll();
    	FacesMessage msg = new FacesMessage(result);  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }  
}
